package aole.util;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;

public class Util {

	public static BufferedImage copyImage( BufferedImage bi ) {
		if( bi == null ) return null;
		ColorModel cm = bi.getColorModel();
		boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
		WritableRaster raster = bi.copyData( null );
		return new BufferedImage( cm, raster, isAlphaPremultiplied, null );
	}

	public static BufferedImage copyImage( BufferedImage bi, int x, int y,
			int width, int height ) {
		if( bi == null ) return null;
		ColorModel cm = bi.getColorModel();
		boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
		WritableRaster raster = bi.copyData( null );
		return (new BufferedImage( cm, raster, isAlphaPremultiplied, null ))
				.getSubimage( x, y, width, height );
	}
}
